﻿using Shop.Collect;
using Shop.Model.GoodsSku;
using Shop.RemoteModel.Goods.Model;
using Shop.Service.Interface;
using WeDonekRpc.Helper;

namespace Shop.Service.Extend
{
    internal class GoodsSkuStockService : IGoodsSkuStockService
    {
        private readonly IGoodsSkuCollect _Sku;
        private readonly IGoodsSkuStateCollect _SkuState;
        private readonly IGoodsStockCollect _Stock;

        public GoodsSkuStockService (IGoodsSkuCollect sku,
            IGoodsSkuStateCollect skuState,
            IGoodsStockCollect stock)
        {
            this._Sku = sku;
            this._SkuState = skuState;
            this._Stock = stock;
        }

        public GoodsStock[] GetStocks (long goodsId)
        {
            SkuBase[] skus = this._Sku.GetsByGoodsId<SkuBase>(goodsId);
            Dictionary<string, bool> state = this._SkuState.GetsSkuState(goodsId);
            Dictionary<long, int> stocks = this._Stock.GetStocks(goodsId);
            return skus.Convert(c => state.GetValueOrDefault(c.SpecsKey, true), c => new GoodsStock
            {
                SkuId = c.SkuId,
                GoodsSku = c.GoodsSku,
                SkuName = c.SkuName,
                Stock = stocks.GetValueOrDefault(c.SkuId, 0),
                SpecShow = c.SpecsShow
            });
        }
    }
}
